﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">
	<xsl:output method="html" indent="no"/>
	<xsl:param name="RefinementPanelCaption"></xsl:param>
	<xsl:param name="QueryId"></xsl:param>
	<xsl:param name="TagsCategoryId"></xsl:param>
	<xsl:param name="MetadataServiceUrl"></xsl:param>
	<xsl:param name="IsUIRightToLeft"></xsl:param>
	<xsl:param name="ApplyButtonText"></xsl:param>
	<xsl:param name="CancelButtonText"></xsl:param>
	<xsl:param name="CurrentLcid"></xsl:param>
	<xsl:param name="MoreLinkPanelIDSuffix"></xsl:param>
	<xsl:param name="IdPrefix" />
	<xsl:param name="IsDesignMode">True</xsl:param>
	<xsl:param name="RefineByHeading">Refinement</xsl:param>
	<xsl:param name="IsRTL">False</xsl:param>

	<xsl:template match="FilterPanel">
		<xsl:variable name="FilterCategories" select="FilterCategory" />
		<xsl:variable name="CategoryCount" select="count($FilterCategories)" />
		<xsl:if test="($CategoryCount &gt; 0)">
			<xsl:call-template name="FilterCategory">
				<xsl:with-param name="Categories" select="$FilterCategories" />
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

	<xsl:template name="FilterCategory">
		<xsl:param name="Categories" />
		<xsl:if test="$RefinementPanelCaption != ''">
			<div class="ms-searchref-caption">
				<xsl:value-of select="$RefinementPanelCaption" />
			</div>
		</xsl:if>
		<xsl:for-each select="$Categories">
			<xsl:variable name="FilterCategoryId" select="concat(@Id,$IdPrefix)"/>
			<xsl:variable name="TagsId" select="concat($TagsCategoryId,$IdPrefix)"/>
			<xsl:variable name="ColumnId" select="substring(@Id,10)"/>
			<xsl:variable name="FilterCategoryType" select="@Type"/>
			<xsl:variable name="IsEnhanced" select="@IsEnhanced"/>
			<xsl:variable name="HideAny" select="@HideAny" />
			<xsl:variable name="Collapsed" select="@Collapsed" />
			<xsl:variable name="ManagedProperty" select="@ManagedProperty"/>
			<xsl:variable name="SSPList" select="substring-before(CustomData/AssociateTermSets, '|')"/>
			<xsl:variable name="TermSetList" select="substring-after(CustomData/AssociateTermSets, '|')"/>
			<xsl:variable name="ShowMoreLink" select="translate(@ShowMoreLink,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
			<xsl:variable name="FreeFormFilterHint" select="@FreeFormFilterHint"/>
			<xsl:variable name="MoreLinkText" select="@MoreLinkText"/>
			<xsl:variable name="LessLinkText" select="@LessLinkText"/>
			<xsl:variable name="ShowCounts" select="@ShowCounts"/>
			<xsl:variable name="DisplayName" select="@DisplayName"/>
			<xsl:variable name="ShowTaggingControl" select="@ShowTaggingControl"/>
			<xsl:variable name="SelectionType" select="@SelectionType" />

			<div id="{concat('refiner_',$ManagedProperty)}">
				<xsl:choose>
          <!-- 折り畳みアクション -->
					<xsl:when test="$Collapsed = 'true'">
						<div class="ms-searchref-categoryname" style="cursor:pointer; text-decoration: underline;" onclick="{concat('ToggleCategory(', &quot;'&quot;,$FilterCategoryId,&quot;'&quot;,')')}">
							<xsl:value-of select="$DisplayName"/>
							<xsl:text> </xsl:text>
							<div id="collapseimg" class="collapsedcategory">
							</div>
						</div>
					</xsl:when>
					<xsl:otherwise>
						<div class="ms-searchref-categoryname" style="cursor:pointer; text-decoration: underline;" onclick="{concat('ToggleCategory(', &quot;'&quot;,$FilterCategoryId,&quot;'&quot;,')')}">
							<xsl:value-of select="$DisplayName"/>
							<xsl:text> </xsl:text>
							<div id="collapseimg" class="expandedcategory">
							</div>
						</div>

					</xsl:otherwise>
				</xsl:choose>
        
				<xsl:variable name="collapsedDisplay">
					<xsl:choose>
						<xsl:when test="$Collapsed = 'true'">
							<xsl:text>none</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>block</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
        
				<div class="{concat('refiner_data_',$FilterCategoryId, ' ','refiner_data_',$ManagedProperty)}" style="display:{$collapsedDisplay}">
						<ul class="ms-searchref-filters" id="TopFilters_{$FilterCategoryId}">
							<xsl:choose>
                <!-- グラフ表示の場合 IsEnhanced = 'True' -->
								<xsl:when test="$IsEnhanced = 'true'">
									<xsl:choose>
										<xsl:when test="$HideAny = 'true'">
										</xsl:when>
										<xsl:otherwise>
											<xsl:call-template name="Filter">
												<xsl:with-param name="Filters" select="Filters/Filter[string-length(Count)=0]" />
												<xsl:with-param name="ShowCounts" select="$ShowCounts" />
												<xsl:with-param name="IsEnhanced" select="$IsEnhanced" />
											</xsl:call-template>
										</xsl:otherwise>
									</xsl:choose>
									<xsl:value-of select="EnhancedHtml" disable-output-escaping="yes"/>
								</xsl:when>
								<xsl:when test="$FilterCategoryType = 'Message'">
									<xsl:for-each select="Filters/Filter">
										<xsl:call-template name="FilterMessage">
											<xsl:with-param name="Value" select="Value" />
										</xsl:call-template>
									</xsl:for-each>
								</xsl:when>
								<xsl:otherwise>                  
									<xsl:call-template name="Filter">
										<xsl:with-param name="ManagedProperty" select="$ManagedProperty"/>
										<xsl:with-param name="Filters" select="Filters/Filter" />
										<xsl:with-param name="ShowCounts" select="$ShowCounts" />
										<xsl:with-param name="SelectionType" select="$SelectionType"/>
									</xsl:call-template>

                  <!-- マルチ選択対応 -->
                  <xsl:choose>
                    <xsl:when test="$SelectionType = 'MultiValue'">
                      <div class="ms-multivalue-select-div" id="{$ManagedProperty}">
                        <input class="multivalue-select-button" type="button" value="適用" herf="" id="{$ManagedProperty}">
                          <!-- 隠し項目描写 -->
                          <xsl:call-template name="hiddenRender">
                            <xsl:with-param name="ManagedProperty" select="$ManagedProperty"/>
                            <xsl:with-param name="Filters" select="Filters/Filter" />
                            <xsl:with-param name="SelectionType" select="$SelectionType"/>
                          </xsl:call-template>
                        </input>
                        <br />
                      </div>
                    </xsl:when>
                  </xsl:choose>

                </xsl:otherwise>
							</xsl:choose>
						</ul>
						<xsl:if test="$ShowMoreLink='TRUE'">
							<xsl:variable name="MoreFilters" select="MoreFilters/Filter" />
							<xsl:choose>
								<xsl:when test="$FilterCategoryId and ($FilterCategoryId != '') and ($FilterCategoryType = 'Microsoft.Office.Server.Search.WebControls.ManagedPropertyFilterGenerator')">
									<xsl:variable name="MoreDivId" select="concat('RefinementMore_', $QueryId, $FilterCategoryId)" />
									<xsl:variable name="LessDivId" select="concat('RefinementLess_', $QueryId, $FilterCategoryId)" />
									<xsl:if test="$MoreFilters != ''">
										<a class="ms-searchref-more" href="javascript:{{}}" onclick="SearchEnsureSOD();ToggleRefMoreLessFilters(this, true);">
											<div class="ms-searchref-morelink">
												<xsl:value-of select="$MoreLinkText" />
												<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
												<img src="/_layouts/images/more_arrow.png" class="ms-searchref-moreicon" />
											</div>
										</a>
										<ul class="ms-searchref-filters" id="MoreFilters_{$FilterCategoryId}" style="display:none">
											<xsl:call-template name="Filter">
												<xsl:with-param name="ManagedProperty" select="$ManagedProperty"/>
												<xsl:with-param name="Filters" select="$MoreFilters" />
												<xsl:with-param name="ShowCounts" select="$ShowCounts" />
												<xsl:with-param name="SelectionType" select="$SelectionType"/>
											</xsl:call-template>
										</ul>
										<a class="ms-searchref-more" href="javascript:{{}}" onclick="SearchEnsureSOD();ToggleRefMoreLessFilters(this, false);" style="display:none">
											<div class="ms-searchref-morelink">
												<xsl:value-of select="$LessLinkText" />
												<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
												<img src="/_layouts/images/less_arrow.png" class="ms-searchref-moreicon" />
											</div>
										</a>
									</xsl:if>
								</xsl:when>
								<xsl:when test="$FilterCategoryId and ($FilterCategoryId != '') and ($FilterCategoryType = 'Microsoft.Office.Server.Search.WebControls.TaxonomyFilterGenerator')">
									<xsl:variable name="IsTagsColumn" select="$FilterCategoryId = $TagsId" />
									<xsl:if test="$MoreFilters != ''">
										<a id="{$MoreLinkPanelIDSuffix}_{$FilterCategoryId}" href="javascript:{{}}" class="ms-searchref-more" onclick="SearchEnsureSOD();RenderTaggingControl('{$FilterCategoryId}', {$IsTagsColumn}, '{$SSPList}', '{$TermSetList}', '{$MetadataServiceUrl}', {$CurrentLcid}, '{$MoreLinkPanelIDSuffix}', '{$DisplayName}', {$ShowTaggingControl}, {$IsUIRightToLeft});">
											<div class="ms-searchref-morelink">
												<xsl:value-of select="$MoreLinkText" />
												<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
												<img src="/_layouts/images/more_arrow.png" class="ms-searchref-moreicon" />
											</div>
										</a>
										<div style="display:none">
											<ul class="ms-searchref-filters" id="MoreFilters_{$FilterCategoryId}">
												<xsl:call-template name="Filter">
													<xsl:with-param name="ManagedProperty" select="$ManagedProperty"/>
													<xsl:with-param name="Filters" select="MoreFilters/Filter" />
													<xsl:with-param name="ShowCounts" select="$ShowCounts" />
													<xsl:with-param name="SelectionType" select="$SelectionType"/>
												</xsl:call-template>
											</ul>
											<div id="TaxonomyMoreControl_{$FilterCategoryId}">
												<input id="MetadataHiddenInput_{$FilterCategoryId}" type="hidden" />
												<div id="TaggingControl_{$FilterCategoryId}" class="ms-taxonomy">
												</div>
												<img style="display:none" src="/_layouts/images/RTE2FIND.gif" class="ms-searchref-taxapply" align="right" alt="{$ApplyButtonText}" onclick="SearchEnsureSOD();var link=GetTaxonomyApplyFilterUrl('{$FilterCategoryId}','{$ColumnId}');if (link!='')window.location=link;" />
											</div>
										</div>
										<a style="display:none" class="ms-searchref-more" href="javascript:{{}}" onclick="SearchEnsureSOD();ToggleTaxonomyLessFilters(this);">
											<div class="ms-searchref-morelink">
												<xsl:value-of select="$LessLinkText" />
												<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
												<img src="/_layouts/images/less_arrow.png" class="ms-searchref-moreicon" />
											</div>
										</a>
									</xsl:if>
								</xsl:when>
								<xsl:when test="$FilterCategoryId and ($FilterCategoryId != '') and ($FilterCategoryType = 'Message')">
								</xsl:when>
								<xsl:otherwise>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:if>
						<div class="ms-searchref-catseparator">&#160;</div>
					</div>
			</div>

		</xsl:for-each>
	</xsl:template>

	<xsl:template name="Filter">
		<xsl:param name="Filters" />
		<xsl:param name="ShowCounts" />
		<xsl:param name="ManagedProperty" />
		<xsl:param name="SelectionType" />
		<xsl:param name="IsEnhanced" />

		<xsl:for-each select="$Filters">
			<xsl:variable name="Selection" select="Selection" />
			<xsl:choose>
				<xsl:when test="($Selection = 'Selected')">
					<xsl:call-template name="FilterLink">
						<xsl:with-param name="ManagedProperty" select="$ManagedProperty"/>
						<xsl:with-param name="Url" select="Url" />
						<xsl:with-param name="UrlTooltip" select="Tooltip" />
						<xsl:with-param name="Value" select="Value" />
						<xsl:with-param name="FilterSelection" select="'ms-searchref-selected ms-searchref-removable'" />
						<xsl:with-param name="ShowCounts" select="$ShowCounts" />
						<xsl:with-param name="Count" select="Count" />
						<xsl:with-param name="Percentage" select="Percentage" />
						<xsl:with-param name="Indentation" select="Indentation" />
						<xsl:with-param name="Selection" select="$Selection" />
						<xsl:with-param name="SelectionType" select="$SelectionType"/>
            <xsl:with-param name="BoolFirst" select="@isFirst"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:when test="($Selection = 'Implied')">
					<xsl:call-template name="FilterLink">
						<xsl:with-param name="ManagedProperty" select="$ManagedProperty"/>
						<xsl:with-param name="Url" select="Url" />
						<xsl:with-param name="UrlTooltip" select="Tooltip" />
						<xsl:with-param name="Value" select="Value" />
						<xsl:with-param name="FilterSelection" select="'ms-searchref-selected'" />
						<xsl:with-param name="ShowCounts" select="$ShowCounts" />
						<xsl:with-param name="Count" select="Count" />
						<xsl:with-param name="Percentage" select="Percentage" />
						<xsl:with-param name="Indentation" select="Indentation" />
						<xsl:with-param name="Selection" select="$Selection" />
						<xsl:with-param name="SelectionType" select="$SelectionType"/>
            <xsl:with-param name="BoolFirst" select="@isFirst"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:when test="($Selection = 'Static')">
					<xsl:call-template name="FilterLink">
						<xsl:with-param name="ManagedProperty" select="$ManagedProperty"/>
						<xsl:with-param name="Url" select="Url" />
						<xsl:with-param name="UrlTooltip" select="Tooltip" />
						<xsl:with-param name="Value" select="Value" />
						<xsl:with-param name="FilterSelection" select="'ms-searchref-unselected static'" />
						<xsl:with-param name="ShowCounts" select="$ShowCounts" />
						<xsl:with-param name="Count" select="Count" />
						<xsl:with-param name="Percentage" select="Percentage" />
						<xsl:with-param name="Indentation" select="Indentation" />
						<xsl:with-param name="Selection" select="$Selection" />
						<xsl:with-param name="SelectionType" select="$SelectionType"/>
            <xsl:with-param name="BoolFirst" select="@isFirst"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="FilterLink">
						<xsl:with-param name="ManagedProperty" select="$ManagedProperty"/>
						<xsl:with-param name="Url" select="Url" />
						<xsl:with-param name="UrlTooltip" select="Tooltip" />
						<xsl:with-param name="Value" select="Value" />
						<xsl:with-param name="FilterSelection" select="'ms-searchref-unselected'" />
						<xsl:with-param name="ShowCounts" select="$ShowCounts" />
						<xsl:with-param name="Count" select="Count" />
						<xsl:with-param name="Percentage" select="Percentage" />
						<xsl:with-param name="Indentation" select="Indentation" />
						<xsl:with-param name="Selection" select="$Selection" />
						<xsl:with-param name="SelectionType" select="$SelectionType"/>
            <xsl:with-param name="BoolFirst" select="@isFirst"/>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="FilterLink">
		<xsl:param name="Url"/>
		<xsl:param name="UrlTooltip"/>
		<xsl:param name="Value"/>
		<xsl:param name="FilterSelection"/>
		<xsl:param name="ShowCounts"/>
		<xsl:param name="Count"/>
		<xsl:param name="Percentage"/>
		<xsl:param name="Indentation"/>
		<xsl:param name="ManagedProperty"/>
		<xsl:param name="Selection"/>
		<xsl:param name="SelectionType" />
    <xsl:param name="BoolFirst" />

		<xsl:variable name="SecureUrl">
			<xsl:call-template name="GetSecureUrl">
				<xsl:with-param name="Url" select="$Url" />
			</xsl:call-template>
		</xsl:variable>
		<li class="ms-searchref-filter {$FilterSelection}">
			<xsl:if test="($Indentation = '1')">
				<span class="ms-searchref-indenticon">&#8627;&#160;</span>
			</xsl:if>
			<xsl:choose>
				<xsl:when test="$SelectionType = 'MultiValue'">
					<xsl:choose>
						<xsl:when test="$Selection = 'Implied'">
              <xsl:choose>
                <xsl:when test="$BoolFirst = 'True'">
                  <a class="ms-searchref-filterlink-Allthis" href="" title="{$RefineByHeading}: {$UrlTooltip}" id="{$ManagedProperty}">
                    <xsl:value-of select="Value"/>
                  </a>
                </xsl:when>
                <xsl:otherwise>
                  <input class="ms-searchref-filterlink-checkbox" type="checkbox" id="{$ManagedProperty}" name="{concat($ManagedProperty,'[]')}" checked="true"/>
                  <a class="ms-searchref-filterlink ms-searchref-filterlink-checkbox-link" title="{$RefineByHeading}: {$UrlTooltip}">
                    <xsl:value-of select="Value"/>
                  </a>
                </xsl:otherwise>
              </xsl:choose>              
						</xsl:when>
						<xsl:otherwise>
							<input class="ms-searchref-filterlink-checkbox" type="checkbox" id="{$ManagedProperty}" name="{concat($ManagedProperty,'[]')}"/>
              <a class="ms-searchref-filterlink ms-searchref-filterlink-checkbox-link" title="{$RefineByHeading}: {$UrlTooltip}">
                <xsl:value-of select="Value"/>
              </a>
						</xsl:otherwise>
					</xsl:choose>
					<!-- <a class="ms-searchref-filterlink ms-searchref-filterlink-checkbox-link" href="" title="{$RefineByHeading}: {$UrlTooltip}"> -->
				</xsl:when>
				<xsl:otherwise>
					<xsl:choose>
						<xsl:when test="$Selection = 'Static'">
							<xsl:value-of select="Value"/>
						</xsl:when>
						<xsl:otherwise>
							<a class="ms-searchref-filterlink" href="{$SecureUrl}" title="{$RefineByHeading}: {$UrlTooltip}">
								<xsl:value-of select="Value"/>
							</a>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:choose>
				<xsl:when test="($ShowCounts = 'Count') and ($Count != '')">
					<span class="ms-searchref-count">
						<xsl:if test="$IsRTL = 'True'">&#x200f;</xsl:if>
						(<xsl:value-of select="Count"/>)
					</span>
				</xsl:when>
				<xsl:when test="($ShowCounts = 'Percentage') and ($Percentage != '')">
					<span class="ms-searchref-count">
						<xsl:if test="$IsRTL = 'True'">&#x200f;</xsl:if>
						(<xsl:value-of select="format-number($Percentage, '0%')"/>)
					</span>
				</xsl:when>
			</xsl:choose>
		</li>
	</xsl:template>

	<xsl:template name="FilterMessage">
		<xsl:param name="Value"/>
		<xsl:param name="FilterSelection"/>
		<li class="ms-searchref-filtermsg">
			<span class="ms-searchref-filterlink ms-searchref-msg">
				<xsl:value-of select="Value"/>
			</span>
		</li>
	</xsl:template>

	<xsl:template name="GetSecureUrl">
		<xsl:param name="Url"/>
		<xsl:choose>
			<xsl:when test="$IsDesignMode = 'True'">
				<xsl:value-of select="$Url" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="ddwrt:EnsureAllowedProtocol($Url)" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
  
  <xsl:template name="hiddenRender">
    <xsl:param name="Filters" />
    <xsl:param name="ManagedProperty" />
    <xsl:param name="SelectionType" />

    <xsl:variable name="resultValue">
      <xsl:for-each select="$Filters">
        <xsl:variable name="Selection" select="Selection" />
        <xsl:variable name="UrlTooltip" select="Tooltip" />
        <xsl:choose>
          <xsl:when test="$SelectionType = 'MultiValue'">
            <xsl:choose>
              <xsl:when test="$Selection = 'Implied'">
                <xsl:value-of select="concat('&#44;','&quot;',$UrlTooltip,'&quot;')"/>
              </xsl:when>
            </xsl:choose>
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="result" select="$resultValue"/>
    <input class="multivalue-select-hiddenvalue" type="hidden" value= "{$result}" id="multihidden_{$ManagedProperty}"/>
  </xsl:template>

</xsl:stylesheet>
